<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 异常订单ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

require 'module/base/billHandler.class.php';
require 'module/base/logHandler.class.php';

class ajax_billException {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询异常订单
                    $this->searchBillException();
                    break;

                case 2: //异常取消订单
                    $this->cancelBill();
                    break;

                case 3: //重新发货
                    $this->againDelivery();
                    break;

            }
            
        }
        
    }

    /**
     * 取消订单
     */
    private function cancelBill() {

        global $G_PGLOB;

        //实例化订单类
        $billHandler = new billHandler( $G_PGLOB->DBCA );

        //取消订单
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $_REQUEST['isException'] = 1;
        $result_array = $billHandler->cancelBill( $_REQUEST );
        if($result_array['code'] == 200){
            //更新异常状态
            $result_array = $billHandler->updateBillExceptionStatus($_REQUEST);
            if($result_array['code'] == 200){
                $result_array['message'] = "订单取消成功";
            }
        }

        echo json_encode($result_array);

    }

    /**
     * 查询异常订单信息
     */
    private function searchBillException() {

        global $G_PGLOB;

        //实例化资源位类
        $billHandler = new billHandler( $G_PGLOB->DBCA );

        $bill_info_array = $billHandler->searchBillException( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($bill_info_array) {
            //设置文本编码
            mb_internal_encoding("UTF-8");

            foreach($bill_info_array as $bill) {

                $operation = '';
                if($bill["handle_status"]){
                    $handle_status = "已处理";
                    //操作
                    $operation .= "";
                }else{
                    //$handle_status = "<a href='javascript:void(0);' onclick='bill.changeStatus(".$bill['bill_no'].");' >否</a>";
                    $handle_status = "未处理";
                    $operation .= '<a href="javascript: void(0);" onclick="bill.delivery(\'' . $bill['uuid'] . '\' , \'' . $bill['bill_no'] . '\' );">重新发货</a>&nbsp;&nbsp;';
                    $operation .= '<a href="javascript: void(0);" onclick="bill.alter( \'' . $bill['bill_no'] . '\' );">修改</a>&nbsp;&nbsp;';
                    $operation .= '<a href="javascript: void(0);" onclick="bill.cancel(\'' . $bill['uuid'] . '\' , \'' . $bill['bill_no'] . '\' );">取消</a>';
                }

                //异常类型
                switch($bill["exception_type"]){
                    case 1:
                        $bill["exception_type"] = "仓库踢回";
                        break;
                    case 2:
                        $bill["exception_type"] = "物流异常";
                        break;
                    case 3:
                        $bill["exception_type"] = "系统错误";
                        break;
                }

                //处理异常原因字符窜
                $width = 30;        //字符串换行长度
                $len = mb_strlen($bill['remark']);
                $remark = '';
                for ($i=0;$i<$len;$i += $width){
                    $remark .=  mb_substr($bill['remark'],$i,$width).($len-$i >= $width ? "<br>" : '' );
                }

                //处理备注字符窜
                $width = 15;        //字符串换行长度
                $len = mb_strlen($bill['handler_remark']);
                $handler_remark = '';
                for ($i=0;$i<$len;$i += $width){
                    $handler_remark .=  mb_substr($bill['handler_remark'],$i,$width).($len-$i >= $width ? "<br>" : '' );
                }


                $result_array['rows'][] = array(
                    'id' => $bill['bill_no'],
                    'cell' => array(
                        $operation,
                        $handle_status,
                        "<a href='javascript:void();' onclick='E.open(\"/quanliadmin/app/bill/bill.pscript?operFlg=2&bill_no=".$bill['bill_no']."\", \"\", {height: 600, width: 1024, top: 10, left: 100});' >".$bill['bill_no']."</a>",
                        $bill['billTypeName'],
                        $bill['createTime'],
                        $bill["exception_type"],
                        $remark,
                        $bill['creator'],
                        $bill['batch_no'],
                        $bill['package_no'],
                        $bill['handler_user'],
                        $bill['handler_time'],
                        $bill['handler_remark']
                    )
                );
            }
        }

        echo json_encode($result_array);

    }


    /**
     * 重新发货
     */
    private function againDelivery(){
        global $G_PGLOB;

        //实例化订单类
        $billHandler = new billHandler( $G_PGLOB->DBCA );

        //取消订单
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //消息参数
        $message_array = array(
            'action' => 'package',
            'operFlg' => 1,
            'params' => array(
                $_REQUEST['bill_no']
            )
        );

        //更新异常状态
        $result_array = $billHandler->updateBillExceptionStatus($_REQUEST);
        if($result_array['code'] == 200){
            //更新成功后推送消息
            $result_array = sendMessageMq($message_array, 2);
            if($result_array['code'] != 200 ){
                error_log($result_array['message']);
                $result_array['message'] = "链接超时，请重试";
            }
        }

        echo json_encode($result_array);

    }


}